<template>
  <view>
    <my-search @click="goSearch"></my-search>
    <view class="scroll-view-container">
      <!-- 左侧的滑动区域 -->
      <scroll-view class="left-scroll-view" scroll-y="true" :style="{height:screenHeight+'px'}">
        <!-- 这里用block,不然格式会乱 -->
        <block v-for="(item,index) in cataList" :key="index">
          <view @click="activeItem(index)" :class="['left-scroll-view-item',index==activeIndex? 'active':'']">
          {{item.cat_name}}
        </view>
        </block>
        
      </scroll-view>
      <!-- 右侧的滑动区域 -->
      <scroll-view scroll-y="true" :style="{height:screenHeight+'px'}" :scrollTop="scrollTop">
        <!-- 二级分类 -->
        <view class="cate-lv2" v-for="(item1,index1) in childList" :key="index1">
         <view class="cate-lv2-title">/{{item1.cat_name}}/</view>
        <!-- 三级分类 -->
        <view class="cate-lv3-list">
          <view class="cate-lv3-item" v-for="(item2,index2) in item1.children" :key="index2" @click="goGoodsList(item2)">
            <image :src="item2.cat_icon" mode="widthFix"></image>
            <text>{{item2.cat_name}}</text>
          </view>
        </view>
         </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 当前设备可用的高度
        screenHeight:0,
        cataList:[],
        activeIndex:0,
        // 二级分类的列表
        childList:[],
        // 滚动条位置
        scrollTop:0
      };
    },
    methods:{
      getCateList(){
        uni.request({
          url:'https://api-hmugo-web.itheima.net/api/public/v1/categories',
          method:'GET',
          success:(res)=>{
            console.log(res)
            this.cataList = res.data.message
            // 获取二级分类的列表数据
            this.childList = res.data.message[0].children
          }
        })
      },
      activeItem(value){
        this.activeIndex = value
        // 为二级分类列表重新赋值
        this.childList = this.cataList[value].children
        // 重置滚动条位置
        this.scrollTop = this.scrollTop===0? 1:0
      },
      goGoodsList(value){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+value.cat_id
        })
      },
      goSearch(){
       uni.navigateTo({
         url:'/subpkg/search/search'
       })
      }
    },
    onLoad() {
      const res = uni.getWindowInfo()
      // -50是因为设置了搜索框列表,他的高度为50px
      this.screenHeight = res.windowHeight-50
      this.getCateList()
    }
    
  }
</script>

<style lang="scss">
.scroll-view-container{
  display: flex;
  height: 300rpx;
  .left-scroll-view{
    width: 120px;
    .left-scroll-view-item{
      background-color: #F7F7F7;
      line-height: 60px;
      text-align: center;
      font-size: 12px;
      &.active{
        background-color: #FFFFFF;
        position: relative;
        &::before{
          content: '';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #C00000;
          position: absolute;
          top: 50%;
          left: 0;
          transform: translateY(-50%);
        }
      }
    }
  }
}
.cate-lv2-title{
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  padding: 15px 0;
}
.cate-lv3-list{
display: flex;
flex-wrap: wrap;
  .cate-lv3-item{
width: 33.33%;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
margin-bottom: 10px;
    image{
      width: 60px;
      height: 60px;
    }
    text{
      font-size: 12px;
    }
  }
}
</style>
